#!/usr/bin/python
# -*- coding: utf-8 -*-
# coding: utf-8

import MySQLdb
import os

try:
	import configDB
except:
	print "Impossible d'inclure le fichier de configuration, avez vous copier configDB.dist.py en configDB.py ?"
	exit(1)

def majuscule(text):
	return text[0:1].upper()+text[1:len(text)]
	
db=MySQLdb.connect(configDB.serverMySQL, configDB.userMySQL, configDB.passWordMySQL, "information_schema")
c=db.cursor()
c.execute("SET NAMES 'utf8'")
c.execute("SET CHARACTER SET utf8")

c.execute("SELECT TABLE_NAME, TABLE_COMMENT FROM TABLES WHERE TABLE_SCHEMA = '%s'" % (configDB.baseMySQL));

rows=c.fetchall()

for row in rows:
	className="CDAO%s" % (majuscule(row[0]))
	fileName="__"+className+".h"
	if os.path.exists(fileName):
		os.remove(fileName)
	
	f=open(fileName, "w")
	
	fileContent=""
	fileContent+="/**\r\n"
	fileContent+="* \\file "+fileName+"\r\n"
	fileContent+="* \\author Corentin Le Bail\r\n"
	fileContent+="* \\date 2009-2012\r\n"
	fileContent+="* \\brief Fichier de déclaration de la class "+className+"\r\n"
	fileContent+="*/\r\n"
	fileContent+="//------------------------------------------------------------------------------\r\n"
	fileContent+="#ifndef __%s_H__\r\n" % (className.upper())
	fileContent+="#define __%s_H__\r\n" % (className.upper())
	fileContent+="//------------------------------------------------------------------------------\r\n"
	
	constructeur=""
	constructeur+="\t\t/**\r\n"
	constructeur+="\t\t * \\fn "+className+"(void)\r\n"
	constructeur+="\t\t * \\brief Constructeur\r\n"
	constructeur+="\t\t */\r\n"
	constructeur+="\t\t%s(void) : CDAO() {\r\n" % (className)
	
	private="\tprivate:\r\n";
	
	public="\tpublic:\r\n"
	
	public+="\t\t/**\r\n"
	public+="\t\t * \\fn  QString getTableName(void)\r\n"
	public+="\t\t * \\return La nom de la table de la base de données\r\n"
	public+="\t\t */\r\n"
	public+="\t\tQString getTableName(void) { return \"%s\"; }\r\n" % (row[0])
	
	fileContent+="#include \"CDAO.h\"\r\n"
	fileContent+="//------------------------------------------------------------------------------\r\n"
	fileContent+="/**\r\n"
	fileContent+="* \\class "+className+" "+fileName+" \""+fileName+"\"\r\n"
	fileContent+="* \\brief Classe : %s.\r\n" % (row[1])
	fileContent+="*/\r\n"
	fileContent+="class %s : public CDAO {\r\n" % (className)
	c.execute("SELECT COLUMN_NAME, COLUMN_KEY, COLUMN_COMMENT FROM COLUMNS WHERE TABLE_SCHEMA='%s' AND TABLE_NAME='%s'" % (configDB.baseMySQL, row[0]))
	fields=c.fetchall()
	
	setter=""
	setter+="\t\tvirtual void set(QString fieldName, QVariant value) {\r\n"
	
	getter=""
	getter+="\t\tvirtual QVariant get(QString fieldName) {\r\n"
	
	for field in fields:
		if field[1] == "PRI":
			constructeur+="\t\t\taddKey(\"%s\");\r\n" % (field[0])
		
		constructeur+="\t\t\taddField(\"%s\");\r\n" % (field[0])
		private+="\t\tQVariant _%s;\r\n" % (field[0])
		public+="\t\t/**\r\n"
		public+="\t\t * \\fn QVariant getField%s(void) \r\n" % (majuscule(field[0]));
		public+="\t\t * \\return %s\r\n" % (field[2])
		public+="\t\t */\r\n"
		public+="\t\tQVariant getField%s(void) { return _%s; }; \r\n" % (majuscule(field[0]), field[0]);
		public+="\t\t/**\r\n"
		public+="\t\t * \\fn QVariant void setField%s(QVariant %s) \r\n" % (majuscule(field[0]), field[0]);
		public+="\t\t * \\brief affecte %s\r\n" % (field[2])
		public+="\t\t */\r\n"
		public+="\t\tvoid setField%s(QVariant %s) { _%s=%s; }; \r\n" % (majuscule(field[0]), field[0], field[0], field[0]);
		
		setter+="\t\t\tif(fieldName == \"%s\") {\r\n" % (field[0])
		setter+="\t\t\t\tsetField%s(value);\r\n" % (majuscule(field[0]))
		setter+="\t\t\t}\r\n";
		
		getter+="\t\t\tif(fieldName == \"%s\") {\r\n" % (field[0])
		getter+="\t\t\t\treturn getField%s();\r\n" % (majuscule(field[0]))
		getter+="\t\t\t}\r\n";
			
	constructeur+="\t\t}\r\n"
	
	setter+="\t\t}\r\n"
	
	getter+="\t\t\treturn QVariant();\r\n";
	getter+="\t\t}\r\n"
	
	public+=constructeur
	public+=setter
	public+=getter;
	
	fileContent+=private
	fileContent+=public
	
	fileContent+="};\r\n"
	fileContent+="//------------------------------------------------------------------------------\r\n"
	fileContent+="#endif //__%s_H__\r\n" % (className.upper())
	fileContent+="//------------------------------------------------------------------------------\r\n"
	
	f.write(fileContent)
	f.close()